<?php

function get_the_image($args) {
    /* Search the post's content for the <img /> tag and get its URL. */
    preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $args, $matches);

    /* If there is a match for the image, return its URL. */
    if (isset($matches) && $matches[1][0])
        return array('url' => $matches[1][0]);
    return false;
}

function replace_name($param) {
    if (preg_match('/news.hetkar.com/', $param)) {
        $blog = 'news.hetkar.com';
        $to = 'hetkar.com';
    } elseif (preg_match('/khmer108.blogspot.com/', $param)) {
        $blog = 'khmer108.blogspot.com/';
        $to = 'hetkar.com/1/';
    } elseif (preg_match('/loy-news.blogspot.com/', $param)) {
        $blog = 'loy-news.blogspot.com/';
        $to = 'hetkar.com/2/';
    } elseif (preg_match('/newstomo.blogspot.com/', $param)) {
        $blog = 'newstomo.blogspot.com/';
        $to = 'hetkar.com/3/';
    }
    $link = str_replace($blog, $to, $param);
    return $link;
}

$blogid = (!empty($_GET['id']) ? $_GET['id'] : 'news.hetkar.com');

//include ('simple_html_dom.php');
$GETblog = 'http://'.$blogid . '/feeds/posts/default?max-results=25';
$blogid = simplexml_load_file($GETblog);
@header('Content-type: text/xml; charset=UTF-8');
@header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 300));
@header("Cache-Control: no-cache");
@header("Pragma: no-cache");
$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>';
$rssfeed .= '<rss version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>My RSS feed</title>';
$rssfeed .= '<link>http://www.mywebsite.com</link>';
$rssfeed .= '<description>This is an example RSS feed</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>Copyright (C) 2014 hetkar.com</copyright>';
$array = array();
foreach ($blogid->entry as $value) {
    $xmlns = $value->children('http://www.w3.org/2005/Atom');
    $title = $value->title;
    $publish = $value->published;
    $content = $value->content;
    $link = $value->link;
    foreach ($link as $value_link) {
        $link = $value_link->attributes();
        if ($link['rel'] == 'alternate') {
            $link = $link['href'];
        }
    }
    $link = replace_name($link);
    $thumbnails = get_the_image($content);
    $thumbnails = $thumbnails['url'];
    @$thumbnails = $thumbnails;
    $desc_thumb = '<p><img src="' . $thumbnails . '" ';
    $desc_thumb .= ' /></p>';
    $desc = '<![CDATA[' . $desc_thumb . ']]>';
    $rssfeed .= '<item>';
    $rssfeed .= '<title>' . $title . '</title>';
    $rssfeed .= '<description>' . $desc . '</description>';
    $rssfeed .= '<link>' . $link . '</link>';
    $rssfeed .= '<image>
     <url>' . $thumbnails . '</url>
     <title>' . $title . '</title>
     <link>' . $link . '</link>
   </image>';
    $rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($publish)) . '</pubDate>';
    $rssfeed .= '</item>';
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
echo $rssfeed;
?>